#!/bin/bash

# Test cluster snapshot

. ./common
for i in `seq 0 5`; do
	_start_sheep $i;
done
_wait_for_sheep 6

TMPDIR="$STORE/tmp"
rm -rf $TMPDIR

_cluster_format -c 6

_vdi_create test1 10M
$DOG vdi create test2 -c 3 10M

_random | $DOG vdi write test1
_random | $DOG vdi write test2
$DOG vdi read test1 | md5sum > $STORE/csum.11.org
$DOG vdi read test2 | md5sum > $STORE/csum.21.org
$DOG vdi snapshot test1
$DOG vdi snapshot test2
$DOG cluster snapshot save s1 $TMPDIR
$DOG cluster snapshot list $TMPDIR | _filter_date

_random | $DOG vdi write test1
_random | $DOG vdi write test2
$DOG vdi read test1 | md5sum > $STORE/csum.12.org
$DOG vdi read test2 | md5sum > $STORE/csum.22.org
$DOG vdi snapshot test1
$DOG vdi snapshot test2
$DOG cluster snapshot save s2 $TMPDIR
$DOG cluster snapshot list $TMPDIR | _filter_date

_vdi_list

_cleanup
for i in `seq 0 5`; do
	_start_sheep $i;
done
_wait_for_sheep 6

_cluster_format -c 6
$DOG cluster snapshot load s1 $TMPDIR
_vdi_list

$DOG vdi read test1 | md5sum > $STORE/csum.11.new
$DOG vdi read test2 | md5sum > $STORE/csum.21.new
diff -u $STORE/csum.11.org $STORE/csum.11.new
diff -u $STORE/csum.21.org $STORE/csum.21.new

_cleanup
for i in `seq 0 5`; do
	_start_sheep $i;
done
_wait_for_sheep 6

$DOG cluster snapshot load s2 $TMPDIR
_vdi_list

_cleanup
for i in `seq 0 5`; do
	_start_sheep $i;
done
_wait_for_sheep 6

$DOG cluster snapshot load 2 $TMPDIR
_vdi_list

$DOG vdi read -s 2 test1 | md5sum > $STORE/csum.12.new
$DOG vdi read -s 2 test2 | md5sum > $STORE/csum.22.new
diff -u $STORE/csum.12.org $STORE/csum.12.new
diff -u $STORE/csum.22.org $STORE/csum.22.new

$DOG vdi read test1 | md5sum > $STORE/csum.12.new
$DOG vdi read test2 | md5sum > $STORE/csum.22.new
diff -u $STORE/csum.12.org $STORE/csum.12.new
diff -u $STORE/csum.22.org $STORE/csum.22.new

$DOG vdi read -s 1 test1 | md5sum > $STORE/csum.11.new
$DOG vdi read -s 1 test2 | md5sum > $STORE/csum.21.new
diff -u $STORE/csum.11.org $STORE/csum.11.new
diff -u $STORE/csum.21.org $STORE/csum.21.new

$DOG cluster info -v | _filter_cluster_info
_vdi_create test3 10M
_vdi_list


# save or load specified vdi

_cleanup
for i in `seq 0 5`; do
	_start_sheep $i
done
_wait_for_sheep 6

_cluster_format

for i in `seq 0 9`; do
	$DOG vdi create test$i 10M
	_random | $DOG vdi write test$i
	$DOG vdi read test$i | md5sum > $STORE/csum.${i}3.org
	$DOG vdi snapshot test$i -s snap
done
_vdi_list

$DOG cluster snapshot save s3 $TMPDIR test0 test2 test4 test6 test8

$DOG cluster snapshot show s3 $TMPDIR

_cleanup
for i in `seq 0 5`; do
	_start_sheep $i
done
_wait_for_sheep 6

_cluster_format
$DOG cluster snapshot load s3 $TMPDIR
_vdi_list

for i in 0 2 4 6 8; do
	$DOG vdi read test$i | md5sum > $STORE/csum.${i}3.new
	diff -u $STORE/csum.${i}3.org $STORE/csum.${i}3.new
done

_cleanup
for i in `seq 0 5`; do
	_start_sheep $i
done
_wait_for_sheep 6

_cluster_format
$DOG cluster snapshot load s3 $TMPDIR test0 test4 test8
_vdi_list

for i in 0 4 8; do
	$DOG vdi read test$i | md5sum > $STORE/csum.${i}3.new
	diff -u $STORE/csum.${i}3.org $STORE/csum.${i}3.new
done


# delete vdi snapshot before saving cluster snapshot

echo "yes" | _cluster_format
$DOG vdi create test 10M
_random | $DOG vdi write test
$DOG vdi snapshot -s vs1 test
$DOG cluster snapshot save s4 $TMPDIR
$DOG vdi delete -s 1 test
$DOG cluster snapshot save s5 $TMPDIR
$DOG vdi snapshot -s vs2 test
$DOG cluster snapshot save s5 $TMPDIR
echo "yes" | $DOG cluster snapshot load s5 $TMPDIR
_vdi_list
echo "yes" | $DOG cluster snapshot load s5 $TMPDIR test
_vdi_list
